package net.gyapp.markim.module.contact;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import com.easyder.wrapper.base.view.WrapperSwipeActivity;
import com.easyder.wrapper.core.model.BaseVo;

import net.gyapp.markim.ApiConfig;
import net.gyapp.markim.R;
import net.gyapp.markim.core.entity.ConversationEntity;
import net.gyapp.markim.module.basic.base.CommonPresenter;
import net.gyapp.markim.module.basic.event.GroupChanged;
import net.gyapp.markim.module.basic.event.GroupIml;
import net.gyapp.markim.utils.RequestParams;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import me.winds.widget.usage.TitleView;

/**
 * Auther:  winds
 * Email:   heardown@163.com
 * Date:    2018/8/4.
 * Desc:
 */

public class ProjectSetActivity extends WrapperSwipeActivity<CommonPresenter> {


    @BindView(R.id.tv_title)
    EditText tvTitle;
    @BindView(R.id.tv_address)
    EditText tvAddress;

    ConversationEntity entity;
    String title;
    String url;

    public static Intent getIntent(Context context, ConversationEntity entity, String title, String url) {
        return new Intent(context, ProjectSetActivity.class).putExtra("entity", entity).putExtra("title", title).putExtra("url", url);
    }

    @Override
    protected int getViewLayout() {
        return R.layout.activity_project_set;
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        titleView.setCenterText("设置小程序");
        getContainerView().setBackgroundColor(Color.parseColor("#FFFFFF"));
        entity = (ConversationEntity) intent.getSerializableExtra("entity");
        title = intent.getStringExtra("title");
        url = intent.getStringExtra("url");
        tvTitle.setText(title);
        tvAddress.setText(url);
    }

    @Override
    protected void loadData(Bundle savedInstanceState, Intent intent) {

    }

    @Override
    public void showContentView(String url, BaseVo dataVo) {
        if (url.contains(ApiConfig.API_SET_SMALL_PROGRAM)) {
            showToast("设置成功");
            EventBus.getDefault().post(new GroupChanged(GroupIml.SIGN_PROJECT_CHANGED));
            finish();
        }
    }


    /**
     * 设置群小程序
     *
     * @param groupNumber
     * @param groupTitle
     * @param groupTitleUrl
     */
    private void setGroupProject(String groupNumber, String groupTitle, String groupTitleUrl) {
        presenter.setNeedDialog(true);
        presenter.postData(ApiConfig.API_SET_SMALL_PROGRAM, new RequestParams().put("groupNumber", groupNumber).put("groupTitle", groupTitle).put("groupTitleUrl", groupTitleUrl).get(), BaseVo.class);
    }


    @OnClick(R.id.btn_confirm)
    public void onViewClicked() {
        String projectTitle = tvTitle.getText().toString().trim();
        if (TextUtils.isEmpty(projectTitle)) {
            showToast("请输入小程序标题");
            return;
        }

        String projectUrl = tvAddress.getText().toString().trim();
        if (TextUtils.isEmpty(projectTitle)) {
            showToast("请输入小程序网址");
            return;
        }

        if (title.equals(projectTitle) && url.equals(projectUrl)) {
            showToast("和已设置的小程序相同, 请修改");
            return;
        }

        setGroupProject(entity.sessionIdStr, projectTitle, projectUrl);
    }
}
